/************************************************************
* @file   gkt_buff.h
************************************************************/

#ifndef GKT_BUFF_H
#define GKT_BUFF_H

#include "gkt_types.h"
#include "gkt_debug.h"

#ifdef __cplusplus
extern "C" {
#endif

/* buffer header */
typedef struct _gkt_buff {
	uint8_t	*ptr;
	uint32_t	len;
	uint32_t	size;
	uint8_t	data[__ARRAY_EMPTY];
} gkt_buff_t;

__STATIC_INLINE uint32_t gkt_buff_size(gkt_buff_t *buff)
{
	return buff->size - (uint32_t)(buff->ptr - buff->data);
}

__STATIC_INLINE void gkt_buff_forward(gkt_buff_t *buff, uint32_t size)
{
	GKT_ASSERT(size <= buff->len);
	buff->ptr += size;
	buff->len -= size;
}
__STATIC_INLINE void gkt_buff_forward_tail(gkt_buff_t *buff)
{
	buff->ptr += buff->len;
	buff->len = 0;
}

__STATIC_INLINE void gkt_buff_backword(gkt_buff_t *buff, uint32_t size)
{
	GKT_ASSERT(size <= (buff->ptr - buff->data));
	buff->ptr -= size;
	buff->len += size;
}
__STATIC_INLINE void gkt_buff_backword_head(gkt_buff_t *buff)
{
	buff->len += (uint32_t)(buff->ptr - buff->data);
	buff->ptr = buff->data;
}

#ifdef __cplusplus
}
#endif

#endif	/* GKT_BUFF_H */

